import { request } from "../../request/index";
import regeneratorRuntime from "../../lib/runtime/runtime"

Page({
    data: {
        tabs: [
            { id: 0, value: "综合", isActive: true },
            { id: 1, value: "销量", isActive: false },
            { id: 2, value: "价格", isActive: false }
        ],
        goodsList: [],
        totalPage: 1,
    },

    QueryParams: {
        query: "",
        cid: "",
        pagenum: 1,
        pagesize: 10
    },

    onLoad: function (options) {
        this.QueryParams.cid = options.cid || ""
        this.QueryParams.query = options.query || ""
     //   this.getGoodsList();
    },

    handleTabsItemChange(e) {
        const { index } = e.detail;

        let { tabs } = this.data;
        tabs.forEach((v, i) => i === index ? v.isActive = true : v.isActive = false)

        this.setData({
            tabs
        })
    },
    async getGoodsList() {
        const result = await request({ url: "/goods/search", data: this.QueryParams });
        console.log(result);
        const total = result.total
        this.totalPage = Math.ceil(total / this.QueryParams.pagesize)
        this.setData({
            goodsList: [...this.data.goodsList, ...result.goods],

        })

        //关闭下拉刷新
        wx.stopPullDownRefresh();
          
    },

    onReachBottom() {
        if (this.QueryParams.pagenum >= this.totalPage) {
            wx.showToast({ title: '没有下一页数据了' });
        } else {
            this.QueryParams.pagenum++;
            this.getGoodsList();
        }
    },


    onPullDownRefresh() {
        this.setData({
            goodsList: []
        })
        this.QueryParams.pagenum = 1;
        this.getGoodsList();
    }
})